package cn.parllay.dingdangproject.utils;

/**
 * Created by sunny on 2017/8/17.
 */

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.GlideDrawableImageViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;

import java.io.File;

import cn.parllay.dingdangproject.R;
import jp.wasabeef.glide.transformations.CropCircleTransformation;


public class GlideUtils {

    /**
     * 加载完成回调接口
     */
    public interface OnfinishListener {

        void loadFinish();
    }

    /**
     * 从本地加载圆形图片
     *
     * @param context
     * @param imagePath
     * @param imageView
     * @param listener
     */
    public static void loadCircleImage(Context context, String imagePath, ImageView imageView, final OnfinishListener listener) {

        File file = new File(imagePath);
        Glide
                .with(context)
                .load(file)
                .bitmapTransform(new CropCircleTransformation(context))
                .into(new GlideDrawableImageViewTarget(imageView) {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> animation) {
                        super.onResourceReady(resource, animation);

                        if (listener != null) {
                            listener.loadFinish();
                        }

                    }
                });
    }

    /**
     * 从网络加载圆形图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void loadCircleImage(Context context, String url, ImageView imageView) {

        Glide
                .with(context)
                .load(url)
                .bitmapTransform(new CropCircleTransformation(context))
                .error(R.drawable.user_photo)
                .into(imageView);
    }

    /**
     * 从本地加载圆角矩形
     *
     * @param context
     * @param imagePath
     * @param imageView
     * @param listener
     */
    public static void loadRoundedRectangle(final Context context, String imagePath, final ImageView imageView, int with, int height, final float cornerRadius,
                                            final OnfinishListener listener) {

        File cacheFile = new File(imagePath);
        Glide
                .with(context)
                .load(cacheFile)
                .asBitmap()
                .override(with, height)
                .centerCrop()
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {

                        RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                        circularBitmapDrawable.setCornerRadius(cornerRadius);
                        imageView.setImageDrawable(circularBitmapDrawable);

                        if (listener != null) {
                            listener.loadFinish();
                        }
                    }

                });
    }


    /**
     * 从网络加载圆角图片
     * @param context
     * @param url
     * @param imageView
     * @param with
     * @param height
     * @param cornerRadius
     */
    public static void loadRoundedRectangle(final Context context, String url, final ImageView imageView,
                                            int with, int height, final float cornerRadius) {

        Glide
                .with(context)
                .load(url)
                .asBitmap()
                .override(with, height)
                .centerCrop()
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {

                        RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                        circularBitmapDrawable.setCornerRadius(cornerRadius);
                        imageView.setImageDrawable(circularBitmapDrawable);
                    }

                });
    }


    /**
     * 从网络加载图片
     *
     * @param context
     * @param imageView
     * @param url
     */
    public static void loadImage(Context context, final ImageView imageView, String url, final OnfinishListener listener) {

        Glide
                .with(context)
                .load(url)
                .asBitmap()
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        imageView.setImageBitmap(resource);
                        if (listener != null) {
                            listener.loadFinish();
                        }
                    }
                });
    }

    //校验图片url
    public static String checkUrl(String imageUrl){
        if(imageUrl == null) {
            return "";
        }
        if(!imageUrl.startsWith("http")) {
            return "http://120.27.20.74:8080/face/pic/"+imageUrl+".jpg";
        }else {
            return imageUrl;
        }
    }

}